package by.ad.filter;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import static by.ad.HttpUtils.DATE;
import static by.ad.HttpUtils.HttpDateHeaderFormat;

public class ResponseHeaderFilter
  extends ParametrizedFilter
{
  @Override
  public void doFilter(final ServletRequest req, final ServletResponse resp,
    final FilterChain filterChain)
      throws IOException, ServletException
  {
    if (resp instanceof HttpServletResponse)
      for (final String paramName : params.keySet())
        ((HttpServletResponse) resp).setHeader(paramName,
          DATE.equalsIgnoreCase(paramName) ? 
            HttpDateHeaderFormat.getValue() : params.get(paramName));

    filterChain.doFilter(req, resp);
  }
}
